package edu.sctu.practice.chengongsuo.network;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2015/11/26.
 */
public class FictionClient {

    // http://www.bxwx.org/b/70/70093/index.html

    public static final String BASE = "http://www.bxwx.org/b/70/70093/";

    public static void main(String[] args) {

        String index = "index.html";

        List<Fiction> fictions = Lists.newArrayList();

        try {
            Document document = Jsoup.connect(BASE + index).get();

            Elements elements = document.select("dd a");

            for (Element ele : elements) {
//                System.out.println(ele.text());
//                System.out.println(ele.attr("href"));

                Fiction fiction = new Fiction();
                fiction.setTitle(ele.text());
                fiction.setUrl(ele.attr("href"));
                fiction.setContent(getContent(ele.attr("href"), ele.text()));
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getContent(String url, String filename) {
        System.out.println("Request URL: " + BASE + url);
        String content = null;


        String baseDir = "D:\\wanmei\\";
        try {
            Files.createParentDirs(new File(baseDir + "demo.txt"));
            Document document = Jsoup.connect(BASE + url)
                    .timeout(5000).get();

            content = document.select("div#content").get(0).text();

            Files.write(content, new File(baseDir + filename + ".txt"), Charsets.UTF_8);

        } catch (IOException e) {
            e.printStackTrace();
        }


        return content;
    }
}
